//
//  ZCMVAllClassifyViewController.m
//  VoiceMusic
//
//  Created by dllo on 15/10/21.
//  Copyright © 2015年 zhangdeyu. All rights reserved.
//

#import "ZCMVAllClassifyViewController.h"
#import "ZCAFNetWork.h"
#import "ZCMVAllModel.h"
#import <Masonry.h>
#import "ZCMVAllTableViewCell.h"
@interface ZCMVAllClassifyViewController ()

@end

@implementation ZCMVAllClassifyViewController

- (void)dealloc
{
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _typeArray = [[NSMutableArray alloc] init];
    _categaryArray = [[NSMutableArray alloc] init];
    _versionsArray = [[NSMutableArray alloc] init];
    _regionArray = [[NSMutableArray alloc] init];
    [self creatView];
    [self creatload];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(poi:) name:@"return" object:nil];
//     Do any additional setup after loading the view.
    //返回键
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    leftButton.frame = CGRectMake(0, 0, 30, 30);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [leftButton addTarget:self action:@selector(returnClick:) forControlEvents:UIControlEventTouchUpInside];
    
}
#pragma mark 返回
- (void)returnClick:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)poi:(NSNotification *)nsnotification
{
    [self.navigationController popViewControllerAnimated:YES];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:nsnotification.userInfo[@"key"],@"m", nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"name" object:nil userInfo:dic];
}

#pragma mark 创建界面
- (void)creatView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(-40);
    }];
}

#pragma mark 网络解析
- (void)creatload
{
    [ZCAFNetWork getUrl:@"http://api.dongting.com/song/video/type" body:nil result:ZCJSON headerFile:nil success:^(id result) {
        NSDictionary *dic = result;
        self.categaryArray = [ZCMVAllModel baseModelByArr:dic[@"data"]];
        for (NSMutableDictionary *temp in result[@"data"][0][@"subType"]) {
            ZCMVAllModel *model = [[ZCMVAllModel alloc] init];
            [model setValuesForKeysWithDictionary:temp];
            model.ID = [temp objectForKey:@"id"];
            [_typeArray addObject:model];
        }
        for (NSMutableDictionary *tempOne in result[@"data"][1][@"subType"]) {
            ZCMVAllModel *model = [[ZCMVAllModel alloc] init];
            [model setValuesForKeysWithDictionary:tempOne];
            model.ID = [tempOne objectForKey:@"id"];
            [_versionsArray addObject:model];
        }
        for (NSMutableDictionary *tempTwo in result[@"data"][2][@"subType"]) {
            ZCMVAllModel *model = [[ZCMVAllModel alloc] init];
            [model setValuesForKeysWithDictionary:tempTwo];
            model.ID = [tempTwo objectForKey:@"id"];
            [_regionArray addObject:model];
        }
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger i = ((ZCMVAllModel *)self.categaryArray[indexPath.section]).subType.count;
    if (indexPath.section == 3) {
        return 100;
    } else{
        return 50 * i / 3 +10;
    }
}

#pragma mark 实现tableView协议方法
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    ZCMVAllModel *model = self.categaryArray[section];
    return model.name;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.categaryArray.count != 0) {
        return self.categaryArray.count;
    }
    return 0;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cell";
    ZCMVAllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == NO) {
        cell = [[ZCMVAllTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
         if (indexPath.section == 0) {
            cell.self.allArray = self.typeArray;
            [cell.collectionView reloadData];
        } else if (indexPath.section == 1) {
            cell.self.allArray = self.versionsArray;
            [cell.collectionView reloadData];
        } else if (indexPath.section == 2) {
            cell.self.allArray = self.regionArray;
            [cell.collectionView reloadData];
        }
    }
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
